

//
//  HTEmoticonToolBar.swift
//  weibo
//
//  Created by JonsonLiu on 16/4/27.
//  Copyright © 2016年 liujonson. All rights reserved.
//

import UIKit
enum htEmotionType: Int{

    case Recent = 0, Default, Emoji, Lxh
}

class HTEmoticonToolBar: UIStackView {

    var selectButton : UIButton?
    
    var selectSection : Int = 0{
    
        didSet{
        
            let  button = viewWithTag(selectSection) as! UIButton
            
            print(selectSection)
            
            if button != selectButton{
            
                button.selected = true
                selectButton?.selected = false
                selectButton = button
            }
        }
    
    }
    
    var  emtionButtonClickClosed: ((type:htEmotionType) -> ())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()

        
 }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
        tag = 20000
        distribution = .FillEqually
        
        addChildButton("最近", bgimage: "compose_emotion_table_left", type: .Recent)
        addChildButton("默认", bgimage: "compose_emotion_table_mid", type: .Default)
        addChildButton("Emoji", bgimage: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", bgimage: "compose_emotion_table_right", type: .Lxh)

    }
    
    private func addChildButton(title: String , bgimage : String , type :htEmotionType ){
        
        let button = UIButton()
        
        button.addTarget(self, action: "childButtonClick:", forControlEvents: .TouchUpInside)
        
        button.tag = type.rawValue
        
        button.setTitle(title, forState: .Normal)
        
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
    
        button.setBackgroundImage(UIImage(named: "\(bgimage)_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "\(bgimage)_selected"), forState: .Selected)
        
        addArrangedSubview(button)
        
    }
    
    @objc private func childButtonClick(button : UIButton){
    
        if selectButton == button{
        
            return
        
        }
        
        selectButton?.selected = false
        
        button.selected = true
        
        selectButton = button
        
        emtionButtonClickClosed?(type : htEmotionType(rawValue: button.tag)!)
 
    }

}
